// pages/myVipShop/myVipShop.js
const Controllers = require('../controllers/Controller').controller;
const app = getApp();
Page({
    data: {
        globalStype:'',
        selectMyConsumeddata:[],
        islogin:false,
        ishide:true,
        searchstory:[],
        inputValue:'',
				isloading:true,
				tabbar:[]
    },
    onLoad(option) {
		this.setData({
			tabbar:app.globalData.tabbar2
		})
    this.setData({globalStype:app.globalData.globalStype}) 
    },
    onShow(){
      if (wx.getStorageSync('usertoken')) {
          //加载首页数据
          this.selectMyConsumed()
      }
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if(res.data.sessionId){
          this.selectMyConsumed()
        }
      }

    	//从本地获取搜索记录
      let searchstory = wx.getStorageSync('searchData');
      if (searchstory && searchstory.length > 0) {
          //进行倒序
          searchstory = searchstory.reverse();
      }
      this.setData({
          searchstory: searchstory,
          inputValue: '',
      })
    },
    selectMyConsumed(){
    	Controllers.selectMyConsumed().then(res => {
        		for(let item of res.data){
        			if(item.label)
              item.label=item.label.split('/')
        		}
          	this.setData({
          		selectMyConsumeddata:res.data,
          		islogin:true,
          		isloading:false,
          	})
        });
    },
    showall(){
    	if(this.data.ishide){
    		this.setData({ishide:false})
    	}else{
    		this.setData({ishide:true})
    	}
    },
    clearSearchStorage() {
			var that=this;
    	wx.showModal({
	        title: '提示',
	        content: '确定清除历史记录?',
	        success(res) {
	          if (res.confirm) {
					wx.setStorageSync('searchData', []);
					that.setData({
						searchstory: []
					});
	          } else if (res.cancel) {
	            console.log('用户点击取消')
	          }
	        }
	    })
		},
		//跳转店铺详情
		toShop(e){
      let shopId=e.currentTarget.dataset.shopid
      
      wx.requestSubscribeMessage({
        tmplIds:["G1hVNhiSiVctKD70HChIfA_z8cNprccBEjgKRCswY3Y","VahXCIaUZHNHyIIP6LYakcmroqlbOe8Q-qlMLJ5-Pps"],
        complete(){
          wx.reLaunch({
            url: '/pages/Index/Index?shopId='+shopId,
          })
        }
      })
		},

  	bindInput(e){
	    var value=e.detail.value || ''
	    if(value){
	      this.setData({
	        inputValue: value
	      })
	    }
	  },
  	searchvalue(e) {
	    var value=e.currentTarget.dataset.search;
	    wx.navigateTo({
	      url: '/pages/Chain/Lists?search=' + value
	    });
	},
	searchSubmit(e){
	    var _SearchValue=this.data.inputValue;
	    let localStorageValue = [];

	    if (_SearchValue && _SearchValue!=null && _SearchValue.length>=2) {
	        //添加搜索历史记录
	        //调用API从本地缓存中获取数据
	        localStorageValue = wx.getStorageSync('searchData') || [];
	        let tempSearchData = [];
	        //过滤历史列表中相同的搜索记录
	        for (let item of localStorageValue) {
	            if (item != _SearchValue) {
	                tempSearchData.push(item); 
	            }
	        }
	        //添加搜索文本进历史记录
	        tempSearchData.push(_SearchValue);
	        //保存至从本地缓存
	        wx.setStorageSync('searchData', tempSearchData);
          //跳转列表
          wx.navigateTo({
              url: "/pages/Chain/Lists?search=" + _SearchValue
          });
	        
	    }else if(_SearchValue.length<2 && _SearchValue.length>0){

          wx.showToast({
            title: '请输入完整的店铺名称',
            icon:'none'
          })
      }else{
          wx.showToast({
            title: '请输入店铺名称',
            icon:'none'
          })
      }
	    
	},
  onShareAppMessage: function (e) {
  },
  onShareTimeline: function (e) {
	},

	tabChange(e) {
    var index = e.detail.index;
    if (index == 0) {
      wx.reLaunch({
        url: "/pages/Index/Index"
      });
    }
    if (index == 1) {
     
    }
    if (index == 2) {
      wx.reLaunch({
        url: "/pages/my/my-friend/my-friend"
      });
    }
    if (index==3){
      wx.reLaunch({
        url: '/pages/my/my-index/my-index',
      })
    }
  },

  //跳转会员店管理
  toManageShops(){
    wx.navigateTo({
      url: '/pages/Chain/manageShops',
    })
  }
})